<script setup lang="ts">
import { computed } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useExperimentStore } from "@/stores/experimentStore";

const router = useRouter();
const route = useRoute();
const experimentStore = useExperimentStore();

const categoryId = computed(() => route.params.id as string);

const category = computed(() =>
  experimentStore.categories.find((c) => c.id === categoryId.value)
);

const experiments = computed(
  () => experimentStore.experimentsByCategory[categoryId.value] || []
);

// Navigate to experiment page
function goToExperiment(routeName: string) {
  const experiment = experimentStore.experiments.find(
    (exp) => exp.route === routeName
  );
  if (experiment) {
    experimentStore.setCurrentExperiment(experiment.id);

    // 检查该实验是否有专门的路由
    const hasDefinedRoute = router.hasRoute(routeName);

    if (hasDefinedRoute) {
      router.push({ name: routeName });
    } else {
      // 使用通配符路由并传递ID参数
      router.push({
        name: "experiment-coming-soon",
        params: { id: experiment.id },
      });
    }
  }
}

function goHome() {
  router.push("/");
}
</script>

<template>
  <div class="category-view">
    <div v-if="category" class="category-content">
      <div class="category-header">
        <div class="category-icon">{{ category.icon }}</div>
        <div class="category-info">
          <h1 class="category-title">{{ category.name }}</h1>
          <p class="category-stats">
            {{ experiments.length }} 个实验 |
            {{ experiments.filter((e) => e.isImplemented).length }} 个已实现
          </p>
        </div>
      </div>

      <div v-if="experiments.length > 0" class="experiments-list">
        <h2 class="section-title">实验列表</h2>
        <div class="experiments-grid">
          <div
            v-for="experiment in experiments"
            :key="experiment.id"
            class="experiment-card"
            :class="{ 'is-coming-soon': !experiment.isImplemented }"
            @click="goToExperiment(experiment.route)"
          >
            <div class="experiment-icon">{{ experiment.icon }}</div>
            <div class="experiment-content">
              <div class="experiment-header">
                <h3 class="experiment-title">{{ experiment.name }}</h3>
                <span
                  v-if="!experiment.isImplemented"
                  class="status-badge coming-soon"
                >
                  开发中
                </span>
                <span v-else class="status-badge implemented"> 已实现 </span>
              </div>
              <p class="experiment-description">
                {{ experiment.description }}
              </p>
            </div>
            <div class="experiment-action">
              <span class="action-text">{{
                experiment.isImplemented ? "开始实验" : "查看详情"
              }}</span>
              <svg
                xmlns="http://www.w3.org/2000/svg"
                class="action-icon"
                viewBox="0 0 20 20"
                fill="currentColor"
              >
                <path
                  fill-rule="evenodd"
                  d="M10.293 5.293a1 1 0 011.414 0l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414-1.414L12.586 11H5a1 1 0 110-2h7.586l-2.293-2.293a1 1 0 010-1.414z"
                  clip-rule="evenodd"
                />
              </svg>
            </div>
          </div>
        </div>
      </div>
      <div v-else class="empty-state">
        <p class="empty-message">该分类下暂无实验</p>
      </div>
    </div>

    <div v-else class="category-not-found">
      <div class="not-found-icon">🔍</div>
      <h2 class="not-found-title">分类不存在</h2>
      <p class="not-found-message">抱歉，您访问的实验分类不存在或已被移除</p>
      <button class="back-button" @click="goHome">返回首页</button>
    </div>
  </div>
</template>

<style scoped>
.category-view {
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.category-header {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #e5e7eb;
  margin-bottom: 2rem;
}

.category-icon {
  font-size: 3rem;
  background-color: #f3f4f6;
  padding: 1rem;
  border-radius: 0.5rem;
}

.category-info {
  flex: 1;
}

.category-title {
  font-size: 1.875rem;
  font-weight: 700;
  color: #111827;
  margin-bottom: 0.5rem;
}

.category-stats {
  font-size: 1rem;
  color: #6b7280;
}

.section-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #111827;
  margin-bottom: 1.5rem;
}

.experiments-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1.5rem;
}

.experiment-card {
  display: flex;
  background-color: #f9fafb;
  border-radius: 0.5rem;
  overflow: hidden;
  cursor: pointer;
  transition:
    transform 0.2s,
    box-shadow 0.2s;
  position: relative;
}

.experiment-card:hover {
  transform: translateY(-3px);
  box-shadow:
    0 4px 6px -1px rgba(0, 0, 0, 0.1),
    0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.experiment-card.is-coming-soon {
  background-color: #fafafa;
  border: 1px dashed #e5e7eb;
}

.experiment-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  padding: 1.5rem;
  background-color: #eff6ff;
  color: #1d4ed8;
}

.experiment-card.is-coming-soon .experiment-icon {
  background-color: #f9fafb;
  color: #9ca3af;
}

.experiment-content {
  flex: 1;
  padding: 1.25rem;
}

.experiment-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.experiment-title {
  font-size: 1.125rem;
  font-weight: 600;
  color: #111827;
  margin: 0;
}

.status-badge {
  font-size: 0.625rem;
  padding: 0.125rem 0.375rem;
  border-radius: 9999px;
  font-weight: 500;
}

.status-badge.coming-soon {
  background-color: #fef3c7;
  color: #d97706;
}

.status-badge.implemented {
  background-color: #d1fae5;
  color: #059669;
}

.experiment-description {
  font-size: 0.875rem;
  color: #4b5563;
  line-height: 1.5;
  margin: 0;
}

.experiment-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  position: absolute;
  bottom: 0;
  right: 0;
  background-color: #eff6ff;
  color: #1d4ed8;
  font-weight: 500;
  font-size: 0.875rem;
  border-top-left-radius: 0.375rem;
}

.experiment-card.is-coming-soon .experiment-action {
  background-color: #f3f4f6;
  color: #6b7280;
}

.action-text {
  margin-right: 0.5rem;
}

.action-icon {
  width: 1rem;
  height: 1rem;
}

.empty-state,
.category-not-found {
  text-align: center;
  padding: 4rem 0;
}

.empty-message {
  font-size: 1.125rem;
  color: #6b7280;
}

.not-found-icon {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.not-found-title {
  font-size: 1.875rem;
  font-weight: 600;
  color: #111827;
  margin-bottom: 1rem;
}

.not-found-message {
  font-size: 1.125rem;
  color: #6b7280;
  max-width: 36rem;
  margin: 0 auto 2rem;
}

.back-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.75rem 1.5rem;
  background-color: #2563eb;
  color: white;
  font-weight: 500;
  border-radius: 0.375rem;
  transition: background-color 0.2s;
  border: none;
  cursor: pointer;
}

.back-button:hover {
  background-color: #1d4ed8;
}

@media (max-width: 640px) {
  .experiments-grid {
    grid-template-columns: 1fr;
  }

  .category-header {
    flex-direction: column;
    text-align: center;
    align-items: center;
    gap: 1rem;
  }

  .category-icon {
    font-size: 2.5rem;
  }

  .category-title {
    font-size: 1.5rem;
  }
}
</style>
